package com.example.hw_calendar;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;

import com.example.hw_calendar_DB.ToDoItemData;
import com.example.hw_calendar_View.ItemDataListView;

public class ListAdapter extends BaseAdapter implements
		ItemDataListView.OnItemDataClickListener {

	Context mContext;
	List<ToDoItemData> mList;

	public interface OnItemDataClickListener{
		public void onItemDataClick(Adapter a, View v, ItemDataListView data);
	}
	
	OnItemDataClickListener mListener;
	
	public void setOnItemDataClickListener(OnItemDataClickListener listener){
		mListener = listener;
	}
	
	public ListAdapter(Context context, ArrayList<ToDoItemData> list) {
		// TODO Auto-generated constructor stub
		mContext = context;
		mList = list;
	}

	public void setToDoItemData(ToDoItemData itemData) {
		mList.add(itemData);
		notifyDataSetChanged();

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ToDoItemData itemData = mList.get(position);
		ItemDataListView listView;

		if (convertView == null) {
			listView = new ItemDataListView(mContext);
		} else {
			listView = (ItemDataListView) convertView;
		}
		listView.setData(itemData);
		return listView;
	}

	@Override
	public void onItemData(View v, ItemDataListView data) {
		// TODO Auto-generated method stub
		if(mListener != null){
			mListener.onItemDataClick(this, v, data);
		}
	}

}
